package top.xana.acg.common.utils

import android.app.Application
import android.content.Context
import android.content.res.Resources
import android.graphics.Outline
import android.util.Log
import android.util.LruCache
import android.util.TypedValue
import android.view.View
import android.view.ViewOutlineProvider
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.navigation.Navigation
import com.alibaba.android.vlayout.DelegateAdapter
import top.xana.acg.common.base.adp.BaseDelegateAdapter

/**
 * Created by xana on 2022/1/9
 * Describe: hello world
 */

fun dp2px(paramFloat: Float) =
    TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        paramFloat,
        Resources.getSystem().displayMetrics
    )

fun stringForTime(msec: Long): String? {
    val sec = (msec / 1000).toInt()
    return String.format("%02d:%02d", sec / 60, sec % 60)
}

fun log(msg: Any?, tag: String = "yukinoa", error: Boolean = false) {
    if (error)
        Log.e(tag, "" + msg.toString())
    else Log.d(tag, "" + msg.toString())
}

fun hintKb(app: Application, view: View?) {
    if (view == null) return
    view.clearFocus()
    val imm =
        app.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}


inline fun DelegateAdapter.addAdapterSeq(vararg r: BaseDelegateAdapter) {
    for (adp in r) {
        addAdapter(adp)
    }
}


private val dpCache = LruCache<Int, Int>(10)
fun View.dp2px(@DimenRes dp: Int): Int {
    if (null == dpCache.get(dp))
        dpCache.put(dp, context.resources.getDimensionPixelOffset(dp))
    return dpCache[dp]!!
}

fun View.radius(@DimenRes id: Int){
    clipToOutline = true
    outlineProvider = object : ViewOutlineProvider(){
        override fun getOutline(p0: View?, p1: Outline?) {
            p0?.let {
                p1?.setRoundRect(0, 0, width, height, dp2px(id).toFloat())
            }
        }
    }
}

fun View.oval(){
    clipToOutline = true
    outlineProvider = object : ViewOutlineProvider(){
        override fun getOutline(p0: View?, p1: Outline?) {
            p0?.let{
                p1?.setOval(0, 0, width, height)
            }
        }
    }
}


fun View.toast(msg: String) = Toast.makeText(context.applicationContext, msg, Toast.LENGTH_SHORT)


fun View.navTo(@IdRes id: Int) = Navigation.findNavController(this).navigate(id)

fun ImageView.setDrawable(@DrawableRes id: Int){
    setImageDrawable(context.getDrawable(id))
}
fun TextView.setDrawableStart(@DrawableRes id: Int) {
    setCompoundDrawablesWithIntrinsicBounds(context.getDrawable(id), null, null, null)
}

fun TextView.setDrawableEnd(@DrawableRes id: Int) {
    setCompoundDrawablesWithIntrinsicBounds(null, null, context.getDrawable(id), null)
}

fun TextView.setDrawableTop(@DrawableRes id: Int) {
    setCompoundDrawablesWithIntrinsicBounds(null, context.getDrawable(id), null, null)
}

fun TextView.setDrawableBottom(@DrawableRes id: Int) {
    setCompoundDrawablesWithIntrinsicBounds(null, null, null, context.getDrawable(id))
}